<?php
/*
 * СПВ
 **/
class SP_Form_CommonForm
{
	protected $name;
	protected $fields=array();
	protected $method='post';
	protected $action='';
	protected $global_array='_REQUEST';
	protected $attr=array();
	protected $errors=array();
	protected $input=1;
	protected $js;
	const INPUT_POST=1;
	const INPUT_GET=2;
	const INPUT_REQUEST=3;
	
	public function __construct($attr=null)
	{
		if(is_array($attr))
		{
			foreach($attr as $key => $value)
			{
				$this->$key=$value;
			}
		}
	}
	
	public function setName($value)
	{
		 $this->name=$value;
		 return $this;
	}
	
	public function getName()
	{
		return $this->name;
	}
	
	public function setField($field)
	{
		$name=$field->getName();
		if( $this->hasField($name) ) throw new exception('Field with name "'.$name.'" already exist');
		$field->setFormName($this->getName());
		$field->setInput($this->input);
		$this->fields[$field->getName()]=$field;
		return $this;
	}
	
	public function setFields($fields)
	{
		foreach($fields as $field)
		{
			$this->setField($field);
		}
	}
	
	public function hasField($name)
	{
		return isset($this->fields[$name]);
	}
	
	public function getField($name)
	{
		if( !$this->hasField($name) ) throw new exception('Form "'.$this->name.'" has not "'.$name.'" field');
		return $this->fields[$name];
	}
	
	public function deleteField($name)
	{
		unset($this->fields[$name]);
		return $this;
	}
	
	public function deleteFields($names)
	{
		foreach($names as $name)
		{
			$this->deleteField($name);
		}
		return $this;
	}
	
	public function setMethod($value)
	{
		 $this->method=$value;
		 return $this;
	}
	
	public function getMethod()
	{
		return $this->method;
	}
	
	public function setAction($value)
	{
		 $this->action=$value;
		 return $this;
	}
	
	public function getAction()
	{
		return $this->action;
	}
	
	public function getLabel($name)
	{
		return $this->getField($name)->getLabel();
	}
	
	public function getError($name)
	{
		return $this->getField($name)->getError();
	}
	
	public function getFormError($name)
	{
		return $this->errors[$name];
	}
	
	public function setFormError($name, $value)
	{
		$this->errors[$name]=$value;
	}
	
	public function getHtml($name)
	{
		return $this->getField($name)->getHtml();
	}
	
	public function getValue($name)
	{
		return $this->getField($name)->getValue();
	}
	
	public function setValue($name, $value)
	{
		$this->getField($name)->setValue($value);
		return $this;
	}
	
	public function getValues()
	{
		$array=array();
		foreach($this->fields as $field)
		{
			$array[$field->getName()]=$field->getValue();
		}
		return $array;
	}

	public function setInput($value)
	{
		$this->input=$value;
		return $this;
	}
	
	public function setAttr($value)
	{
		$this->attr=$value;
		return $this;
	}
	
	public function getAttr()
	{
		return $this->attr;
	}
	
	public function addAttr($name, $value)
	{
		$this->attr[$name]=$value;
		return $this;
	}
	
	public function getFormTagHtml()
	{
		$attr=SP_Helper_Html::makeAttributes($this->attr);
		$out='<form action="'.$this->action.'" method="'.$this->method.'"'.$attr.'>';
		return $out;
	}
	
	public function setJs($value)
	{
		$this->js=$value;
	}
	
	public function getJs()
	{
		return $this->js;
	}
	
	public function getFieldsJs()
	{
		foreach($this->fields as $field)
		{
			$out.=$field->getJs()."\r\n";
		}
		return $out;
	}
	
	public function getFormJsHtml()
	{
		$out='<script type="text/javascript">'.$this->getFieldsJs().'</script>';
		return $out;
	}
	
	public function validate()
	{
		$result=true;
		foreach($this->fields as $field)
		{
			$result = $result * $field->validate();
		}
		return $result;
	}
	
	public function fill($array)
	{
		//echo 'start';
		//var_dump($array);
		foreach($this->fields as $field)
		{
			if( array_key_exists($field->getName(), $array) )
			{
				//echo 'fdf';
				$field->setValue($array[$field->getName()]);
			}
		}
	}
	
	public function isSubmited()
	{
		$array=$this->getInputArray();
		return $array->has($this->getName());
		//return is_array($_REQUEST[$this->getName()]);
	}
	
	protected function &getInputArray()
	{
		if( $this->input==self::INPUT_POST )
		{
			$input = new SP_Helper_Post;
			//return $_POST;
		}
		elseif( $this->input==self::INPUT_GET )
		{
			$input = new SP_Helper_Get;
			//return $_GET;
		}
		elseif( $this->input==self::INPUT_REQUEST )
		{
			$input = new SP_Helper_Request;
			//return $_REQUEST;
		}
		return $input;
	}
	
	public function fromRequest()
	{
		foreach($this->fields as $field)
		{
			$field->setFromRequest();
		}
	}
	
	protected function getGlobal()
	{
		
	}
	
}